package com.github.io.config;

import com.github.io.interceptor.CustomizeHandlerInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * @author liry
 * @version 1.0
 * @date Created on 2022/8/11 14:38
 * Description: 添加自定义组件
 */

@Configuration
//@EnableWebMvc
public class CustomizeWebMvcConfigurer implements WebMvcConfigurer {

    /**
     * @param registry addInterceptor: 需要实现一个 HandlerInterceptor 接口的拦截实例对象
     *                 addPathPatterns: 用于设置拦截器过滤路径规则；
     *                 excludePathPatterns: 用于设置不需要拦截的过滤规则；
     *                 拦截器的主要用途: 进行用户登录状态拦截、日志拦截等
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加自定义 拦截器；并设置拦截器所生效的路径和不生效的路径(即：配置哪些路径的请求需要被拦截，哪些请求可以被放过);
        // 如下示例: 对以 "/user" 开头的请求进行拦截，对以 "/menu"开头的请求直接放行;
        registry.addInterceptor(new CustomizeHandlerInterceptor()).addPathPatterns("/user/**").excludePathPatterns("/menu/**", "/test/**");
    }


    /**
     * 获取请求对应的页面
     *
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 获取访问页面: 当请求是 "/menu/index"时， 需要返回 "index.html" 页面;
        /**
         * @GetMapping("/index")
         * public Object index() {
         *      return "index";
         * }
         */
        registry.addViewController("/menu/index").setViewName("index");

        // 请求重定向，请求是 " /menu " 时则重定向到 " /menu/index "
        registry.addRedirectViewController("/menu", "/menu/index");
    }


    /**
     * @param registry 功能: 自定义静态资源映射目录
     *                 <p>
     *                 addResourceHandler：指的是对外暴露的访问路径
     *                 addResourceLocations：指的是内部文件放置的目录
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("classpath:/static/index");
//                .addResourceLocations("classpath:/META-INF/resources/")
//                .addResourceLocations("classpath:/resources/")
//                .addResourceLocations("classpath:/static/")
//                .addResourceLocations("classpath:/public/");
    }


    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
//        WebMvcConfigurer.super.configureDefaultServletHandling(configurer);
        configurer.enable();
        configurer.enable("defaultServletName");
    }


    /**
     * 配置跨域
     *
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/cors/**")
                .allowedHeaders("*")
                .allowedMethods("POST", "GET")
                .allowedOrigins("*");

    }


    public InternalResourceViewResolver resourceViewResolver() {
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
        //请求视图文件的前缀地址
        internalResourceViewResolver.setPrefix("/WEB-INF/static/");
        //请求视图文件的后缀
        internalResourceViewResolver.setSuffix(".html");
        return internalResourceViewResolver;
    }

    /**
     * 自定义视图解析器
     *
     * @param registry
     */
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.viewResolver(resourceViewResolver());

    }
}
